package com.winit.deploy.task.phpTask;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.util.JSONFileUtil;
import com.winit.core.task.Task;
import com.winit.deploy.bean.WinitPhpInfo;
import com.winit.deploy.server.WinitPhpServer;
import com.winit.project.util.ProjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * php回滚
 * Created by wangjian on 16/10/17.
 */
public class PhpRollBackTask extends Task {

    private static Logger logger = LoggerFactory.getLogger(PhpRollBackTask.class);

    private WinitPhpServer phpServer;

    public PhpRollBackTask(String taskId, Integer expireTime,WinitPhpServer phpServer) {
        super(taskId, expireTime);
        this.phpServer = phpServer;
    }

    @Override
    public TaskStatus call() throws Exception {
        logger.info("com.winit.deploy.task.phpTask.PhpRollBackTask.call start taskId="+this.taskId);
        WinitPhpInfo info = this.phpServer.getPhpInfo();
        String projectName = info.getProjectName();

        String linuxUser = "apache";
        JSONArray array = JSONFileUtil.getFileJsonForBean(Thread.currentThread().getContextClassLoader().getResource("").getPath()
                + "data/ui_list.json", JSONArray.class);
        if(array.contains(ProjectUtil.getTrunkProjectName(projectName))){
            linuxUser = info.getDstUser();
        }

        this.phpServer.doRollBack(linuxUser);
        logger.info("com.winit.deploy.task.phpTask.PhpRollBackTask.call end taskId="+this.taskId);
        return TaskStatus.STATUS_SUCCESS;
    }
}
